package com.zjw.forum.services.impl;

import com.zjw.forum.common.AppResult;
import com.zjw.forum.common.ResultCode;
import com.zjw.forum.dao.ArticleMapper;
import com.zjw.forum.dao.ArticleReplyMapper;
import com.zjw.forum.exception.ApplicationException;
import com.zjw.forum.model.Article;
import com.zjw.forum.model.ArticleReply;
import com.zjw.forum.services.IArticleReplyService;
import com.zjw.forum.services.IArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ArticleReplyServiceImpl implements IArticleReplyService {
    @Autowired
    ArticleReplyMapper articleReplyMapper;
    @Autowired
    ArticleMapper articleMapper;
    @Autowired
    IArticleService articleService;
    @Override
    public int replyArticle(Long articleId, String content,Long postUserId) {
        //参数校验
        if(articleId==null||articleId<=0||!StringUtils.hasLength(content)){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //校验文章是否存在
        Article article = articleMapper.selectByPrimaryKey(articleId);
        if(article==null){
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        //校验文章是否被禁用
        if(article.getState()==1){
            log.error(ResultCode.FAILED_ARTICLE_BANNED.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED));
        }
        //回复内容校验，是否说非法语句什么的。。。
        //构造插入对象
        ArticleReply articleReply=new ArticleReply();
        articleReply.setArticleId(articleId);
        articleReply.setContent(content);
        articleReply.setPostUserId(postUserId);
        //设置默认值
        Date date=new Date();
        articleReply.setCreateTime(date);
        articleReply.setUpdateTime(date);
        articleReply.setLikeCount(0);
        articleReply.setReplyUserId(null);
        articleReply.setReplyId(null);
        articleReply.setDeleteState((byte)0);
        articleReply.setState((byte)0);
        //修改文章表中的回复数
        int i = articleService.addReplayCount(articleId);
        if(i!=1){
            log.warn(ResultCode.ERROR_SERVICES.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        return articleReplyMapper.insertSelective(articleReply);
    }

    @Override
    public List<ArticleReply> displayAllReply(Long articleId) {
        //参数校验
        if(articleId==null||articleId<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //校验文章是否存在
        Article article = articleMapper.selectByPrimaryKey(articleId);
        if(article==null){
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        return articleReplyMapper.selectAllByArticleId(articleId);
    }
}
